package com.lancoo.answer.view.fragment.manual;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.paperUtils.FragmentLoadUtils;
import com.lancoo.answer.view.fragment.composition.answer.CompositionAudioAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionPencilAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionPictureAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionVideoAnswerFragment;
import com.lancoo.answer.widget.SplitView;
import com.lancoo.answer.widget.combinationView.CompositionQueseView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/8 11:52
 * Description:人工组卷简答题作答fragmnet
 */
public class ManualAnswerFragment extends Fragment {

    private Child child;
    private View convertView;
    private static final String TAG = "CompositionAnswerFragme";
    private int currentAnswerType=-1;
    private int kindIndex;
    private int queseIndex;
    private int smallQueseIndex;
    private Ques ques;
    private int loadQueseIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        convertView=inflater.inflate(R.layout.ev_fragment_manu_answer_layout,container,false);
        return convertView;
    }


    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int smallQueseIndex){

        ManualAnswerFragment queseFragment=new ManualAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        Log.e(TAG,"initView"+(child==null));
        if(getArguments()==null){
            return;
        }
//        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        kindIndex = getArguments().getInt("kindIndex");
        queseIndex = getArguments().getInt("queseIndex");
        smallQueseIndex = getArguments().getInt("smallQueseIndex");
        loadQueseIndex = getArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX);

        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(kindBeanListk==null||
                kindBeanListk.get(kindIndex).getQuesList()==null||
                kindBeanListk.get(kindIndex).getQuesList().get(queseIndex)==null||
                kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex)==null){
            return;
        }
        ques = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex);
        child = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex);
        CompositionQueseView queseView=view.findViewById(R.id.composition_quese_view);
        queseView.setCloseTxtAnswer(true);
        queseView.setCloseVideoAnswer(false);
        queseView.setOnAnswerTypeChangeCallback(new CompositionQueseView.OnAnswerTypeChangeCallback() {
            @Override
            public void onAnswerTypeSelected(int position) {
                super.onAnswerTypeSelected(position);
                loadAnswerMethod(position);
            }
        });
        queseView.intData(child);

        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.setEnableShowChildAsk(true);
        queseBodyView.initData(child);
        queseBodyView.setVisibility("f".equals(ques.getTypeNo())?View.GONE:View.VISIBLE);
        if(!TextUtils.equals("f",ques.getTypeNo())){
            queseView.hideSortIndex();
        }
//        loadQuesBodyFragment();
    }

//    /**
//     * 加载大题题干内容
//     */
//    private void loadQuesBodyFragment(){
//        FragmentManager childFragmentManager = getChildFragmentManager();
//        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
//        Fragment fragment= FragmentLoadUtils.loadQuesBodyFragment(ques,kindIndex,queseIndex,smallQueseIndex,loadQueseIndex);
//        fragmentTransaction.replace(R.id.framelayout_topicBody,fragment,TAG);
//        fragmentTransaction.commitAllowingStateLoss();
//    }

    private void loadAnswerMethod(int answerType){
        Log.e(TAG,"initView"+answerType);
//        Log.e(TAG,"initView"+kindIndex+""+queseIndex+""+smallQueseIndex);
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment= getChildFragmentManager().findFragmentByTag(answerType+"");//getFragmentManager()
        Fragment lastFragment= getChildFragmentManager().findFragmentByTag(currentAnswerType+"");//getFragmentManager()
        if(lastFragment!=null){
            Log.e("aaaa","隐藏当前fragment");
            fragmentTransaction.hide(lastFragment);
        }

        if(fragment==null){
            switch (answerType){
                case 1:
                    fragment=CompositionAudioAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex,0);
                    break;
                case 2:
                    fragment=CompositionPictureAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex,0);
                    break;
                case 3:
                    fragment= CompositionVideoAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex);
                    break;
                default:
                    fragment=CompositionPencilAnswerFragment.loadFragment(kindIndex,queseIndex,smallQueseIndex);
            }
            Log.e("aaaa","replace"+answerType);
            fragmentTransaction.replace(R.id.framelayout_answer,fragment,answerType+"");
        }else{
            Log.e("aaaa","show"+answerType);
            fragmentTransaction.show(fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
        currentAnswerType=answerType;
    }


    /**
     * 软键盘弹起，SplitView自动撑大下方布局
     * @param isShow 软件盘是否显示
     */
    public void updateSplitLayout(boolean isShow){
        Log.e(TAG,"updateSplitLayout:"+isShow);
        if(convertView==null){
            return;
        }
        if (convertView.findViewById(R.id.split_view)==null){
            return;
        }

        SplitView splitView = convertView.findViewById(R.id.split_view);
        splitView.postDelayed(() -> splitView.forceAdjsutAnswerHeight(0.7f),100);
    }


}
